package com.dingup.provider.service.wx.impl;

import com.dingup.api.enums.ThirdPartyTypeEnum;
import com.dingup.api.model.sys.SysWxOpenIdInfo;
import com.dingup.api.model.sys.ThirdPartyUserInfo;
import com.dingup.provider.mapper.SysUserBindInfoMapper;
import com.dingup.provider.mapper.SysWxOpenIdInfoMapper;
import com.dingup.provider.mapper.SysWxUserMapper;
import com.dingup.provider.mapper.ThirdPartyUserInfoMapper;
import com.dingup.provider.service.wx.SysWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wancheng on 2018/10/22.
 */
@Service
public class SysWxUserServiceImpl implements SysWxUserService {

    private static final String APP_ID = "wxd38807fe9c097d8e";

    @Autowired
    ThirdPartyUserInfoMapper thirdPartyUserInfoMapper;
    @Autowired
    SysWxOpenIdInfoMapper sysWxOpenIdInfoMapper;
    @Autowired
    SysUserBindInfoMapper sysUserBindInfoMapper;
    @Autowired
    SysWxUserMapper sysWxUserMapper;

    @Override
    public String getWxOpenId(String userId) {
        ThirdPartyUserInfo wxUser = thirdPartyUserInfoMapper.getByUserIdAndThirdPartyType(userId, ThirdPartyTypeEnum.WEI_XIN);

        if (wxUser != null) {
            SysWxOpenIdInfo openIdInfo = sysWxOpenIdInfoMapper.getByUnionIdAndAppid(wxUser.getThirdPartyUserId(), APP_ID);
            if (openIdInfo != null) {
                return openIdInfo.getOpenId();
            }
        }
        return null;
    }

    @Override
    public List<String> getParentWxOpenId(String userId) {

        return sysUserBindInfoMapper.getUserParentId(userId, APP_ID);
    }
}
